﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TravelLink.DAL;
using DevExpress.Web.ASPxGridView;

namespace TravelLink.WebApp.UserControl.Member.JobEditable {
    public class JobEditableSkill : JobEditableBase {


        public JobEditableSkill(bool isEn)
            : base(isEn) {

        }


        public override string AspGridViewName {
            get { return "aspGridSkills"; }
        }


        protected override IEnumerable<object> GetSource(ResumeParam p) {
            if (Has(AspGridViewName)) {
                var memoryList = Get<TL_User_Skills>(u => u.Status != EditableItemStatus.Delete);
                if (memoryList != null) {
                    return memoryList.Select(u => u.Item).ToList();
                }
                return null;
            }
            else {
                var db = DataContextFactory.Create();
                List<EditableItem<TL_User_Skills>> innerList = db.TL_User_Skills
                    .Where(u => u.UserCode == new Guid(p.UserCode) && u.ResumeId == p.ResumeIdx)
                    .Select(u => Add<TL_User_Skills>(u, EditableItemStatus.Default))
                    .ToList();
                Set(AspGridViewName, innerList);
                return innerList.Select(u => u.Item).ToList();
            }
        }



        public override void Save(ResumeParam p) {
            var db = DataContextFactory.Create();

            var memoryList = Get<TL_User_Skills>(u => true);
            var insetedList = memoryList.Where(u => u.Status == EditableItemStatus.Insert);
            foreach (var item in insetedList) {
                db.TL_User_Skills.InsertOnSubmit(new TL_User_Skills {
                    Skill = item.Item.Skill,
                    RowTime = DateTime.Now,
                    UserCode = new Guid(p.UserCode),
                    ResumeId = p.ResumeIdx
                });
            }

            var updateList = memoryList.Where(u => u.Status == EditableItemStatus.Edit);
            foreach (var item in updateList) {
                var skillEntity = db.TL_User_Skills.FirstOrDefault(u => u.Idx == item.Item.Idx);
                if (skillEntity != null) {
                    skillEntity.Skill = item.Item.Skill;
                    skillEntity.ResumeId = p.ResumeIdx;
                }
            }

            var deleteList = memoryList.Where(u => u.Status == EditableItemStatus.Delete);
            foreach (var item in deleteList) {
                db.TL_User_Skills.DeleteAllOnSubmit(db.TL_User_Skills.Where(u => u.Idx == item.Item.Idx));
            }

            db.SubmitChanges();
            Clear(AspGridViewName);
        }

        protected override void PreDeleting(ResumeParam p, DevExpress.Web.Data.ASPxDataDeletingEventArgs e, ASPxGridView sender) {
            var memoryList = Get<TL_User_Skills>(u => true);

            if (memoryList == null) return;

            int key = ((int)e.Keys[0]);

            var entity = memoryList.Where(u => u.Item.Idx == key).FirstOrDefault();
            if (entity.Status != EditableItemStatus.Insert)
                entity.Status = EditableItemStatus.Delete;
            else {
                memoryList.Remove(entity);
            }

            Set(AspGridViewName, memoryList);
        }

        protected override void PreUpdating(ResumeParam p, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e, ASPxGridView sender) {
            var memoryList = Get<TL_User_Skills>(u => true);

            if (memoryList == null) return;

            int key = ((int)e.Keys[0]);
            string skill = e.NewValues["Skill"] == null ? string.Empty : e.NewValues["Skill"].ToString();

            var entity = memoryList.Where(u => u.Item.Idx == key).FirstOrDefault();
            if (entity.Status == EditableItemStatus.Default)
                entity.Status = EditableItemStatus.Edit;
            entity.Item.Skill = skill;

            Set(AspGridViewName, memoryList);
        }

        protected override void PreInserting(ResumeParam p, DevExpress.Web.Data.ASPxDataInsertingEventArgs e, ASPxGridView sender) {
            var memoryList = Get<TL_User_Skills>(u => true);

            if (memoryList == null) return;

            string skill = e.NewValues["Skill"] == null ? string.Empty : e.NewValues["Skill"].ToString();

            EditableItem<TL_User_Skills> entity = new EditableItem<TL_User_Skills>();

            entity.Status = EditableItemStatus.Insert;
            entity.Item = new TL_User_Skills();
            entity.Item.Skill = skill;
            entity.Item.UserCode = new Guid(p.UserCode);
            entity.Item.RowTime = DateTime.Now;
            if (memoryList.Count > 0)
                entity.Item.Idx = memoryList.Select(u => u.Item.Idx).Max() + 1;
            else
                entity.Item.Idx = 9999;

            memoryList.Add(entity);

            Set(AspGridViewName, memoryList);
        }
    }


}